#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2017-04-20 15:35:10
# Project: jd_test

from pyspider.libs.base_handler import *


class Handler(BaseHandler):
    crawl_config = {
        # 'headers': {
        #     'User-Agent': 'Chrome',
        #     'Cookie': 'xxx'
        # },
        # 'proxy': '123.123.123.123:8000'
    }

    @every(minutes=24 * 60)
    def on_start(self):
        self.crawl('m.jd.com', callback=self.index_page, headers={}, proxy='')

    @config(age=10 * 24 * 60 * 60)
    def index_page(self, response):
        for each in response.doc('a[href^="http"]').items():
            link = each.attr.href
            if '/mall/active/' in link:
                self.crawl(link, callback=self.index_page)
            elif '/ware/' in link:
                self.crawl(link, callback=self.detail_page)

    @config(priority=2)
    def detail_page(self, response):
        return {
            "url": response.url,
            "title": response.doc('title').text(),
        }
